package com.weikuang.fareast.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.jzxiang.pickerview.TimePickerDialog;
import com.jzxiang.pickerview.data.Type;
import com.jzxiang.pickerview.listener.OnDateSetListener;
import com.trello.rxlifecycle2.android.ActivityEvent;
import com.weikuang.fareast.R;
import com.weikuang.fareast.bean.DailyCountBean;
import com.weikuang.fareast.http.MainHttp;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.dlc.commonlibrary.okgo.rx.OkObserver;
import cn.dlc.commonlibrary.ui.widget.CommonItemView;
import cn.dlc.commonlibrary.ui.widget.TitleBar;
import cn.dlc.commonlibrary.utils.TimeFormats;
import cn.dlc.commonlibrary.utils.ToastUtil;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class CheckGoodsActivity extends BaseActivity implements OnDateSetListener {
    @BindView(R.id.mTitleBar)
    TitleBar mTitleBar;
    @BindView(R.id.btn_check)
    Button btnCheck;
    @BindView(R.id.btn_goods)
    Button btnGoods;
    @BindView(R.id.tv_now_nums)
    TextView tvNowNums;
    @BindView(R.id.tv_user)
    TextView tvuser;
    @BindView(R.id.tv_common_nums)
    TextView tvCommonNums;
    @BindView(R.id.tv_operation_nums)
    TextView tvOperationNums;
    @BindView(R.id.start_time)
    CommonItemView startTime;

    TimePickerDialog mDialogYearMonthDay;
    @Override
    protected int getLayoutId() {
        return R.layout.activity_check_goods;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
        mTitleBar.leftExit(this);
        mDialogYearMonthDay = new TimePickerDialog.Builder()
                .setType(Type.YEAR_MONTH_DAY)
                .setCallBack(this)
                .build();
    }

    @Override
    protected void onStart() {
        super.onStart();
        dailyCount();
    }

    @OnClick({R.id.btn_check, R.id.btn_goods, R.id.btn_query, R.id.start_time})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_check:
                startActivity(CheckListActivity.class);
                break;
            case R.id.btn_goods:
                startActivity(GoodsActivity.class);
                break;
            case R.id.btn_query:
                startActivity(CheckQueryActivity.class);
                break;
            case R.id.start_time:
                mDialogYearMonthDay.show(getSupportFragmentManager(), "year_month_day");
                break;
        }
    }

    private void dailyCount() {
        String date = startTime.getContentText();
        MainHttp.get().dailyCount(date)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(bindUntilEvent(ActivityEvent.DESTROY))
                .subscribe(new OkObserver<DailyCountBean>() {
                    @Override
                    public void onSuccess(DailyCountBean hotelBean) {
                        showOneToast(hotelBean.msg);
                        tvNowNums.setText(hotelBean.data.amount + "");
                        tvCommonNums.setText(hotelBean.data.amount1 + "");
                        tvOperationNums.setText(hotelBean.data.amount2 + "");
                        tvuser.setText(hotelBean.data.user);
                    }

                    @Override
                    public void onFailure(String message, Throwable tr) {
                        ToastUtil.show(getActivity(), message);
                    }
                });
    }
    @Override
    public void onDateSet(TimePickerDialog timePickerView, long millseconds) {
        startTime.setContent(TimeFormats.formatDateTime1(timePickerView.getCurrentMillSeconds()));
        dailyCount();
    }
}
